package com.hb.trade.server.project.file.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.parent.MPCommonEntity;
import com.hb.core.pojo.project.file.dto.ReviewItemFirstDto;
import com.hb.core.pojo.project.file.dto.ReviewItemThirdDto;
import com.hb.core.pojo.project.file.entity.ReviewItemFirst;
import com.hb.core.pojo.project.file.mapper.ReviewItemFirstMapper;
import com.hb.core.pojo.project.file.mapper.ReviewItemSecondMapper;
import com.hb.core.pojo.project.file.mapper.ReviewItemThirdMapper;
import com.hb.core.pojo.project.section.entity.BidDownRecord;
import com.hb.trade.server.project.file.service.ReviewItemFirstService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fzzn.core.util.IdUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 一级评审项 服务实现类
 * </p>
 *
 * @author tian
 * @since 2022-04-13
 */
@Service
public class ReviewItemFirstServiceImpl extends ServiceImpl<ReviewItemFirstMapper, ReviewItemFirst> implements ReviewItemFirstService {

    @Resource
    private ReviewItemSecondMapper reviewItemSecondMapper;
    @Resource
    private ReviewItemThirdMapper reviewItemThirdMapper;

    /**
     * 查询评审项详情
     *
     * @param itemFirstDto
     * @return java.util.List<com.hb.core.pojo.project.file.dto.ReviewItemFirstDto>
     * @author tian
     * @date 2022-4-13 9:49
     */
    @Override
    @Transactional
    public List<ReviewItemFirstDto> selectReviewItem(ReviewItemFirstDto itemFirstDto) {
        // 先查询招标文件id对应的一级评审项是否存在
        List<ReviewItemFirstDto> reviewItemFirstDtoList = baseMapper.selectReviewItemFirstByFileId(itemFirstDto.getFileId());

        // 如果一级评审项不存在 则会直接添加并返回一级评审项
        if (CollectionUtil.isEmpty(reviewItemFirstDtoList)) {
            // 默认创建初步评审，详细评审，报价评审
            Integer firstReviewItemTypes[] = new Integer[]{1, 2, 3};
            for (Integer firstReviewItemType : firstReviewItemTypes) {
                // 返回数据的信息
                ReviewItemFirstDto reviewItemFirstDto = new ReviewItemFirstDto();

                ReviewItemFirst reviewItemFirst = new ReviewItemFirst();
                reviewItemFirst.setId(IdUtil.getId());
                reviewItemFirst.setFileId(itemFirstDto.getFileId());
                reviewItemFirst.setFirstReviewItemType(firstReviewItemType);
                reviewItemFirst.setCreateId(itemFirstDto.getCreateId());
                reviewItemFirst.setCreateName(itemFirstDto.getCreateName());
                reviewItemFirst.setCreateTime(LocalDateTime.now());
                baseMapper.insert(reviewItemFirst);

                reviewItemFirstDtoList.add(reviewItemFirst.to(reviewItemFirstDto));
            }
        } else {
            // 如果存在一级评审则去查询二级评审（前端只需要一个集合所以直接查询三级）
            for (ReviewItemFirstDto reviewItemFirstDto : reviewItemFirstDtoList) {
                List<ReviewItemThirdDto> reviewItemThirdDtoList = reviewItemThirdMapper.selectReviewItemThirdByFirstReviewItemId(reviewItemFirstDto.getId());

                reviewItemFirstDto.setReviewItemThirdDtoList(reviewItemThirdDtoList);
            }
        }
        return reviewItemFirstDtoList;
    }

    /**
     * 编辑一级评审项
     *
     * @param reviewItemFirstDto
     * @return java.lang.Boolean
     * @author tian
     * @date 2022-4-13 10:55
     */
    @Override
    @Transactional
    public Boolean editReviewItem(ReviewItemFirstDto reviewItemFirstDto) {
        if (CollectionUtil.isEmpty(reviewItemFirstDto.getFirstReviewItemTypeList())) {
            return false;
        }
        try {
            // 数据库存在，前端传值不存在 说明是删除 需要删除当前一级评审项下的全部数据
            List<ReviewItemFirstDto> reviewItemFirstDtoList = baseMapper.selectReviewItemFirstByNotFirstReviewItemType(reviewItemFirstDto);
            if (!CollectionUtil.isEmpty(reviewItemFirstDtoList)) {
                for (ReviewItemFirstDto itemFirstDto : reviewItemFirstDtoList) {
                    Long firstReviewItemId = itemFirstDto.getId();
                    baseMapper.deleteById(firstReviewItemId);
                    Map deleteMap = new HashMap();
                    deleteMap.put("first_review_item_id", firstReviewItemId);
                    reviewItemSecondMapper.deleteByMap(deleteMap);
                    reviewItemThirdMapper.deleteByMap(deleteMap);
                }
            }

            for (Integer firstReviewItemType : reviewItemFirstDto.getFirstReviewItemTypeList()) {
                // 查询是否存在当前传输的一级评审项
                QueryWrapper<ReviewItemFirst> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda()
                        .eq(ReviewItemFirst::getFileId, reviewItemFirstDto.getFileId())
                        .eq(ReviewItemFirst::getFirstReviewItemType, firstReviewItemType).last("limit 1");
                ReviewItemFirst reviewItemFirst = baseMapper.selectOne(queryWrapper);
                // 数据库不存在，前端传值存在，说明是新增，直接添加一级评审项
                if (null == reviewItemFirst) {
                    ReviewItemFirst reviewItemFirstSave = new ReviewItemFirst();
                    reviewItemFirstSave.setId(IdUtil.getId());
                    reviewItemFirstSave.setFileId(reviewItemFirstDto.getFileId());
                    reviewItemFirstSave.setFirstReviewItemType(firstReviewItemType);
                    reviewItemFirstSave.setCreateId(reviewItemFirstDto.getCreateId());
                    reviewItemFirstSave.setCreateName(reviewItemFirstDto.getCreateName());
                    reviewItemFirstSave.setCreateTime(LocalDateTime.now());
                    baseMapper.insert(reviewItemFirstSave);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<ReviewItemFirstDto> selectReviewItemBySectionId(Long sectionId) {
        return baseMapper.selectReviewItemBySectionId(sectionId);
    }

    @Override
    public ReviewItemFirstDto getReviewItemFirst(Long fileId, Integer firstReviewItemType) {
        if (fileId == null || firstReviewItemType == null){
            return null;
        }
        QueryWrapper<ReviewItemFirst> wrapper = ReviewItemFirst.wrapper();
        wrapper.eq("file_id",fileId)
                .eq("first_review_item_type",firstReviewItemType)
                .last("limit 1");
        ReviewItemFirst reviewItemFirst = getOne(wrapper);
        return reviewItemFirst == null ? null : reviewItemFirst.to(ReviewItemFirstDto.class);
    }
}
